package com.pengbei.enshrine.common.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.ProgressBar;

import java.text.DecimalFormat;

public class DownLoadProgressBar extends ProgressBar {
    private boolean isScale = false;
    String text;
    Paint mPaint;

    public DownLoadProgressBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initText();
    }

    public DownLoadProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initText();
    }

    public DownLoadProgressBar(Context context) {
        super(context);
        initText();
    }

    @Override
    public void setLayoutParams(android.view.ViewGroup.LayoutParams params) {
        super.setLayoutParams(params);
    }

    @Override
    public synchronized void setProgress(int progress) {
        // TODO Auto-generated method stub
        setText(progress);
        super.setProgress(progress);

    }
    @Override
    protected synchronized void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        //this.setText();
        Rect rect = new Rect();
        this.mPaint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 8, getResources().getDisplayMetrics()));
        this.mPaint.getTextBounds(this.text, 0, this.text.length(), rect);
        int x = (getWidth() / 2) - rect.centerX();
        int y = (getHeight() / 2) - rect.centerY();
        canvas.drawText(this.text, x, y, this.mPaint);
    }

    //初始化，画笔
    private void initText(){
        this.mPaint = new Paint();
        this.mPaint.setColor(Color.WHITE);

    }

    //设置文字内容
    private void setText(int progress){
//        float max= Float.valueOf(String.valueOf(this.getMax()));
//        float pro= Float.valueOf(String.valueOf(progress));
//        this.text= onepoint(String.valueOf(pro/1024/1024))+"MB/"+ onepoint(String.valueOf(max/1024/1024))+"MB";
        this.text = progress + "%";

    }

    /**
     * 保留一位小数
     * */
    private String onepoint(String string){
        if(!TextUtils.isEmpty(string)){
            DecimalFormat nf = new DecimalFormat();
            nf.setMaximumFractionDigits(1);
            Double d = Double.valueOf(string);
            String str = nf.format(d);
            return str;
        }
        return "0.0";
    }

}